package palidino76.rs2.io;

import palidino76.rs2.player.items.PlayerWeapon;
import palidino76.rs2.player.items.PlayerItems;
import palidino76.rs2.player.items.*;
import palidino76.rs2.player.Player;
import palidino76.rs2.*;
import palidino76.rs2.util.Misc;


public class Frames {
		public void ItemOnInterface(Player p, int interfaceid, int child, int itemsize, int itemid) {
			int inter = ((interfaceid*65536) + child);
			p.stream.createFrame(35);
			p.stream.writeDWord_v2(inter);
			p.stream.writeDWordBigEndian(itemsize);
			p.stream.writeWordBigEndianA(itemid);
		} 
        public void resetGe(Player p, int slot) {
        p.stream.createFrame(137);
        p.stream.writeUnsignedByte(slot);
        p.stream.writeUnsignedByte(0);
        p.stream.writeWord(0);
        p.stream.writeDWord(0);
        p.stream.writeDWord(0);
        p.stream.writeDWord(0);
        p.stream.writeDWord(0);
        }

        public void setGe(Player p, int slot, int progress, int item, int price, int amount, int currentAmount) {
        p.stream.createFrame(137);
        p.stream.writeUnsignedByte(slot);
        p.stream.writeUnsignedByte(progress);
        p.stream.writeWord(item);
        p.stream.writeDWord(price);
        p.stream.writeDWord(amount);
        p.stream.writeDWord(currentAmount);
        p.stream.writeDWord(price * currentAmount);
        }

        public void setGeOnLogin(final Player p) {

    }
   public void setGeSearch(Player p, Object[] o) {
    setConfig1(p, 1109, -1);
    setConfig1(p, 1112, 0);
    setConfig1(p, 1113, 0);
    setInterface(p, 6, 752, 6, 389);
        p.stream.createFrameVarSizeWord(152);
        p.stream.writeString("s");
    String valstring = "s";
        int j = 0;
        for (int i = (valstring.length() - 1); i >= 0; i--) {
            if (valstring.charAt(i) == 115) {
                p.stream.writeString((String) o[j]);
            } else {
                p.stream.writeDWord((Integer) o[j]);
            }
            j++;
        }
        p.stream.writeDWord(570);
        p.stream.endFrameVarSize();
    }

        public void resetItemSlot(Player p, int slot) {
        int item = -1;
        int amount = 0;
        switch(slot) {
        case 0:
            p.frames.setItems(p, -1, -1757, 523, new int[] {item}, new int[] {amount});
            break;
        case 1:
            p.frames.setItems(p, -1, -1758, 524, new int[] {item}, new int[] {amount});
            break;
        case 2:
            p.frames.setItems(p, -1, -1759, 525, new int[] {item}, new int[] {amount});
            break;
        case 3:
            p.frames.setItems(p, -1, -1760, 526, new int[] {item}, new int[] {amount});
            break;
        case 4:
            p.frames.setItems(p, -1, -1761, 527, new int[] {item}, new int[] {amount});
            break;
        case 5:
            p.frames.setItems(p, -1, -1762, 528, new int[] {item}, new int[] {amount});
            break;
        }
    }

        public void setItemSlot(Player p, int slot, int item, int amount) {
        if(amount == 0) {
        return;
        }
        switch(slot) {
        case 0:
            p.frames.setItems(p, -1, -1757, 523, new int[] {item}, new int[] {amount});
            break;
        case 1:
            p.frames.setItems(p, -1, -1758, 524, new int[] {item}, new int[] {amount});
            break;
        case 2:
            p.frames.setItems(p, -1, -1759, 525, new int[] {item}, new int[] {amount});
            break;
        case 3:
            p.frames.setItems(p, -1, -1760, 526, new int[] {item}, new int[] {amount});
            break;
        case 4:
            p.frames.setItems(p, -1, -1761, 527, new int[] {item}, new int[] {amount});
            break;
        case 5:
            p.frames.setItems(p, -1, -1762, 528, new int[] {item}, new int[] {amount});
            break;
        }
    }
    public void setCamera(Player p, int setX, int setY)//Found by laxika
    { 
	p.stream.createFrame(28);
	p.stream.writeWordBigEndian(setX);
	p.stream.writeWordBigEndian(setY);
    }
public void createObject(Player p, int objectId, int height, int objectX, int objectY, int face, int type) {
sendCoords(p, (objectX - ((p.mapRegionX - 6) * 8)), (objectY - ((p.mapRegionY - 6) * 8)));
int ot = ((type << 2) + (face & 3));
p.stream.createFrame(30);
p.stream.writeWordBigEndian(objectId);
p.stream.writeByteA(0);
p.stream.writeByteC(ot);

}
public void createGlobalObject(int objectId, int height, int objectX, int objectY, int face, int type) {
for (Player p : Engine.players) {
if (p == null) {
continue;
}
createObject(p, objectId, height, objectX, objectY, face, type);
createObject(p, 409, 0, 3091, 3506, -2, 10);
createObject(p, 6552, 0, 3094, 3506, -2, 10);
}
}
    public void setPlayerHead(Player p, int interfaceId, int childId) {
	if(p == null || p.stream == null || p.disconnected[0]) {
			return;
		}
        p.stream.createFrame(101);
        p.stream.writeWord(interfaceId);
        p.stream.writeWord(childId);
    }
 /**
     * Sets item options allowed
     * @param p The Player which the frame should be created for.
     * @param set The access mask
     * @param window The window or child interface id
     * @param inter The main interface id
     * @param off The item offset to start with
     * @param len The item count to set
     */
    public void setAccessMask(Player p, int set, int window, int inter, int off, int len) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.stream.createFrame(223);
        p.stream.writeWord(len);
        p.stream.writeWordBigEndianA(off);
        p.stream.writeWordBigEndian(window);
        p.stream.writeWordBigEndian(inter);
        p.stream.writeWordBigEndian(set);
        p.stream.writeWordBigEndian(0);
    }
		/**
     * Runs an ClientScript2 script
     * @param p The Player which the frame should be created for.
     * @param id The script id
     * @param o The script arguments
     * @param valstring The argument types
     */
    public void runScript(Player p, int id, Object[] o, String valstring) {
        if (valstring.length() != o.length) {
            throw new IllegalArgumentException("Argument array size mismatch");
        }
        p.stream.createFrameVarSizeWord(152);
        p.stream.writeString(valstring);
        int j = 0;
        for (int i = (valstring.length() - 1); i >= 0; i--) {
            if (valstring.charAt(i) == 115) {
                p.stream.writeString((String) o[j]);
            } else {
                p.stream.writeDWord((Integer) o[j]);
            }
            j++;
        }
        p.stream.writeDWord(id);
        p.stream.endFrameVarSize();
    }
public int[] interfaces = {654,655,122,177,657,385,275,771,499};

       public void RemovesetInterfaces(Player p) {
        if (p == null || p.disconnected[0]) {
            return;
        }
        setTab(p, 6, 670);
        setTab(p, 11, 670); // Chat options
        setTab(p, 68, 670); // Chatbox
        setTab(p, 64, 670); // HP bar
        setTab(p, 65, 670); // Prayer bar
        setTab(p, 66, 670); // Energy bar
        setTab(p, 67, 670);
        setConfig(p, 1160, -1);
        setTab(p, 8, 670); // Playername on chat
        setTab(p, 73,  670); // Attack tab
        setTab(p, 74, 670); // Skill tab
        setTab(p, 75, 670); //  Quest tab
        setTab(p, 76, 670); // Inventory tab
        setTab(p, 77, 670); // Equipment tab
        setTab(p, 78, 670); // Prayer tab
        setTab(p, 79, 670); // Magic tab
        setTab(p, 80, 670); // Summoning tab <- Enabled
        setTab(p, 81, 670); // Friend tab
        setTab(p, 82, 670); // Ignore tab
        setTab(p, 83, 670); // Clan tab
        setTab(p, 84, 670); // Setting tab
        setTab(p, 85, 670); // Emote tab
        setTab(p, 86, 670); // Music tab
        setTab(p, 87, 670); // Logout tab
    }



    public void connecttofserver(Player p){
        if(p == null || p.stream == null || p.disconnected[0]){
            return;
        }
        p.stream.createFrame(115);
        p.stream.writeByte(2);
    }

    public void sendSentPrivateMessage(Player p, long name, String message) {
        byte[] bytes = new byte[message.length()];
	Misc.encryptPlayerChat(bytes, 0, 0, message.length(), message.getBytes());
	p.stream.createFrameVarSize(89);
	p.stream.writeQWord(name);
	p.stream.writeByte(message.length());
	p.stream.writeBytes(bytes, bytes.length, 0);
	p.stream.endFrameVarSize();
    }

    private static int messageCounter = 1;

    public void sendReceivedPrivateMessage(Player p, long name, int rights, String message) {
        int id = messageCounter++;
	if(id > 16000000) {
            id = 1;
	}
	byte[] bytes = new byte[message.length()+1];
	bytes[0] = (byte) message.length();
	Misc.encryptPlayerChat(bytes, 0, 1, message.length(), message.getBytes());
	p.stream.createFrameVarSize(178);
	p.stream.writeQWord(name);
	p.stream.writeWord(1);
	p.stream.writeByte(((id << 16) & 0xFF));
        p.stream.writeByte(((id << 8 ) & 0xFF));
        p.stream.writeByte(((id      ) & 0xFF));
	p.stream.writeByte(rights);
	p.stream.writeBytes(bytes, bytes.length, 0);
	p.stream.endFrameVarSize();
    }

    public void sendFriend(Player p, long name, int world) {
        p.stream.createFrameVarSize(154);
	p.stream.writeQWord(name);
	p.stream.writeWord(world);
	p.stream.writeByte(1);
	if(world != 0) {
            if(world == 66) {
                p.stream.writeString("World 1");
	    } else {
                p.stream.writeString("RuneScape " + world);
	    }
	}
	p.stream.endFrameVarSize();
    }

    public void sendIgnores(Player p, long[] ignores) {
        p.stream.createFrameVarSizeWord(240);
	for(long ignore : ignores) {
            p.stream.writeQWord(ignore);
	}
	p.stream.endFrameVarSizeWord();
    }

        /**
	 * Plays music for the player
	 * @param p The player the music will play for.
	 * @param song The musicId.
	 */
    public void playMusic(Player p, int Id) {
	if (p == null || p.stream == null || p.disconnected[0]) {
		return;
	}
	p.stream.createFrame(146);
	p.stream.writeWordBigEndianA(Id);
    }

    public void setNPCId(Player p, int npcId, int interfaceId, int childId) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
		p.stream.createFrame(6);
        p.stream.writeWordBigEndian(interfaceId);
        p.stream.writeWordBigEndian(childId);
        p.stream.writeWordBigEndian(npcId);
    }

    public void animateInterfaceId(Player p, int emoteId, int interfaceId, int childId) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
	p.stream.createFrame(245);
        p.stream.writeWordBigEndian(interfaceId);
        p.stream.writeWordBigEndian(childId);
        p.stream.writeWord(emoteId);
    }

    public void addSoundEffect(Player player, int soundId, int bytes, int delay) {
	for (Player p : Server.engine.players) {
		if (p == null || player == null)
			continue;
		p.getByteVector().createFrame(119);
		p.getByteVector().writeWord(soundId);
		p.getByteVector().writeByte(bytes);
		p.getByteVector().writeWord(delay);
	}
    }

    public void playSoundInArea(int playX, int playY, int distance,  int soundId, int j, int delay) {
     for (Player p : Engine.players) {
	if(p == null || p.stream == null || p.disconnected[0]){
		continue;
	}
        if (Misc.getDistance(playX, playY, p.absX, p.absY) <= distance) {
	   p.stream.createFrame(119);
	   p.stream.writeWord(soundId);
	   p.stream.writeByte(j);
	   p.stream.writeWord(delay);
	}
     }
    }


    public void playSound(Player p, int soundId, int j, int delay) { 
	if(p == null || p.stream == null || p.disconnected[0]){
		return;
	}
	p.stream.createFrame(119);
	p.stream.writeWord(soundId);
	p.stream.writeByte(j);
	p.stream.writeWord(delay);
    }

    public void setHintIcon(Player p, int targetType, int targetId, int arrowId, int playerModel) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.stream.createFrame(227);
        int offset = p.stream.outOffset;
        p.stream.writeByte(targetType);
        p.stream.writeByte(arrowId);
        if (targetType == 1 || targetType == 10) {
            p.stream.writeWord(targetId);
            p.stream.outOffset += 3;
        }
        p.stream.writeWord(playerModel);
        for (int i = (p.stream.outOffset - offset); i < 9; i++) {
            p.stream.writeByte(0);
        }
    }

    public void addObject(Player p, int objectId, int height, int objectX, int objectY, int face, int type) {
	sendCoords(p, (objectX - ((p.mapRegionX - 6) * 8)), (objectY - ((p.mapRegionY - 6) * 8)));
	int ot = ((type << 2) + (face & 3));
	p.getByteVector().createFrame(30);
	p.getByteVector().writeWordBigEndian(objectId);
	p.getByteVector().writeByteA(0);
	p.getByteVector().writeByteC(ot);
    }

    public void addStaticObject(int objectId, int height, int objectX, int objectY, int face, int type) {
	for (Player p : Server.engine.players) {
		if (p == null)
			continue;
		if (p.heightLevel == height)
			addObject(p, objectId, height, objectX, objectY, 0, type);
	}
    }  

    public void addLists(Player p) {
    	if(p == null || p.stream == null || p.disconnected[0])
    		return;
    	p.getByteVector().createFrame(115);
    	p.getByteVector().writeByte(2);
    }

    public void addInterfaceItem(Player p, int interfaceid, int child, int itemsize, int itemid) {
	if (p == null || p.stream == null)
		return;
	int inter = ((interfaceid * 65536) + child);
	p.getByteVector().createFrame(35);
	p.getByteVector().writeDWord_v2(inter);
	p.getByteVector().writeDWordBigEndian(itemsize);
	p.getByteVector().writeWordBigEndianA(itemid);
    }

    public void createStaticGraphic(Player p, int graphicId, int absX, int absY) {
	if(p == null || p.stream == null || p.disconnected[0])
		return;
	sendCoords(p, (absX - ((p.mapRegionX - 6) * 8)), (absY - ((p.mapRegionY - 6) * 8)));
	p.getByteVector().createFrame(248);
	p.getByteVector().writeByte(0);
	p.getByteVector().writeWord(graphicId);
	p.getByteVector().writeByte(0);
	p.getByteVector().writeWord(0);
    }

    /**
     * Set either fullscreen or normal.
     * @param p The Player which the frame should be created for.
     * @param set The frame set, 548 for the default setup.
     */
    public void setWindowPane(Player p, int set) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
		if (!p.Resizable) {
        p.stream.createFrame(239);
        p.stream.writeWord(set);
        p.stream.writeByteA(0);
    }
		if (p.Resizable) {
        p.stream.createFrame(239);
        p.stream.writeWord(549);
        p.stream.writeByteA(0);
    }
}

    /**
     * Logs a player out.
     * @param p The Player which the frame should be created for.
     */
    public void logout(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(104);
    }

    /**
     * Display an interface.
     * <p>The various ids determines how the interface is displayed, from an overlay, to covering the chatbox, etc.
     * @param p The Player which the frame should be created for.
     * @param showId Sets the interface such as an overlay, etc.
     * @param windowId What type of window you used, default should be 548.
     * @param interfaceId Where to display it on the screen.
     * @param childId The interface id to display.
     */
    public void setInterface(Player p, int showId, int windowId, int interfaceId, int childId) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(93);
        p.getByteVector().writeWord(childId);
        p.getByteVector().writeByteA(showId);
        p.getByteVector().writeWord(windowId);
        p.getByteVector().writeWord(interfaceId);
    }

    /**
     * Set a players click option.
     * <p>The slot cannot be below 0 and cannot be above 8.
     * @param p The Player which the frame should be created for.
     * @param option The string to set the option to.
     * @param slot The position to set the option on the player.
     */
    public void setPlayerOption(Player p, String option, int slot) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
	p.getByteVector().createFrameVarSize(252);
	p.getByteVector().writeByteC(1);
	p.getByteVector().writeString(option);
	p.getByteVector().writeByteC(slot);
        p.getByteVector().endFrameVarSize();
    }

    /**
     * Setting client configs.
     * <p>This is used for setting prayers, running, etc.
     * @param p The Player which the frame should be created for.
     * @param id The config id to set.
     * @param set What to set the config.
     */
   /* public void setConfig(Player p, int id, int set) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
		p.getByteVector().createFrame(100);
        p.getByteVector().writeWordA(id);
        p.getByteVector().writeByteA(set);
    }*/
	    public void setConfig1(Player p, int id, int set) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
		p.getByteVector().createFrame(100);
        p.getByteVector().writeWordA(id);
        p.getByteVector().writeByteA(set);
    }
		public void setConfig(Player p, int id, int set) {
		if (set < 128) {
			setConfig1(p, id, set);
		} else {
			setConfig2(p, id, set);
		}
	}


    public void setConfig2(Player p, int id, int set) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(161);
        p.getByteVector().writeWord(id);
        p.getByteVector().writeDWord_v1(set);
    }

    public void setBankOptions(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(223);
        p.getByteVector().writeWord(496);
        p.getByteVector().writeWordBigEndianA(0);
        p.getByteVector().writeWordBigEndian(73);
        p.getByteVector().writeWordBigEndian(762);
        p.getByteVector().writeWordBigEndian(1278);
        p.getByteVector().writeWordBigEndian(20);
        p.getByteVector().createFrame(223);
        p.getByteVector().writeWord(27);
        p.getByteVector().writeWordBigEndianA(0);
        p.getByteVector().writeWordBigEndian(0);
        p.getByteVector().writeWordBigEndian(763);
        p.getByteVector().writeWordBigEndian(1150);
        p.getByteVector().writeWordBigEndian(18);
    }

 /**
     * Creates a projectile. Can be used for magic, range etc.
     * @param p The Player which the frame should be created for.
     * @param offsetY The distance between caster and enemy Y
     * @param offsetX The distance between caster and enemy X
     * @param angle The starting place of the projectile
     * @param speed The speed minus the distance making it set.
     * @param gfxMoving The moving graphic ID
     * @param startHeight The starting height
     * @param endHeight The destination height
     * @param lockon The NPC the missile is locked onto.
     * @param npc if this is an npc and not a player set to true
     * */
    public void addProjectile(Player p, int casterY, int casterX, int offsetY, int offsetX, int angle,
            int speed, int gfxMoving, int startHeight, int endHeight, int lockon, boolean npc) {
        if (p == null || p.stream == null) {
            return;
        }
	for (Player pl : Server.engine.players) {
		if (pl != null) {
			if (pl.heightLevel == p.heightLevel) {
        			sendCoords(pl, (casterX - ((pl.mapRegionX - 6) * 8)) - 3, (casterY - ((pl.mapRegionY - 6) * 8)) - 2);
        			pl.getByteVector().createFrame(112);
        			pl.getByteVector().writeByte(angle);
        			pl.getByteVector().writeByte(offsetX);
        			pl.getByteVector().writeByte(offsetY); 
        			pl.getByteVector().writeWordBigEndian(lockon); 
        			pl.getByteVector().writeWord(gfxMoving); 
        			pl.getByteVector().writeByte(startHeight); 
        			pl.getByteVector().writeByte(endHeight); 
        			pl.getByteVector().writeWord(51); 
        			pl.getByteVector().writeWord(speed); 
        			pl.getByteVector().writeByte(16);
        			pl.getByteVector().writeByte(64);
			}
		}
	}						
    }

    public void addProjectile2(Player p, int casterY, int casterX, int offsetY, int offsetX, int angle,
            int speed, int gfxMoving, int startHeight, int endHeight, int lockon, boolean npc) {
        if (p == null || p.stream == null) {
            return;
        }
	for (Player pl : Server.engine.players) {
		if (pl != null) {
			if (pl.heightLevel == p.heightLevel) {
        			sendCoords(pl, (casterX - ((pl.mapRegionX - 6) * 8)) - 3, (casterY - ((pl.mapRegionY - 6) * 8)) - 2);
        			pl.getByteVector().createFrame(112);
        			pl.getByteVector().writeByte(angle);
        			pl.getByteVector().writeByte(offsetX);
        			pl.getByteVector().writeByte(offsetY); 
        			pl.getByteVector().writeWordBigEndian(lockon); 
        			pl.getByteVector().writeWord(gfxMoving); 
        			pl.getByteVector().writeByte(startHeight); 
        			pl.getByteVector().writeByte(endHeight); 
        			pl.getByteVector().writeWord(51); 
        			pl.getByteVector().writeWord(speed); 
        			pl.getByteVector().writeByte(35);
        			pl.getByteVector().writeByte(32);
			}
		}
	}						
    }

    public void addProjectile3(Player p, int casterY, int casterX, int offsetY, int offsetX, int angle,
            int speed, int gfxMoving, int startHeight, int endHeight, int lockon, boolean npc) {
        if (p == null || p.stream == null) {
            return;
        }
	for (Player pl : Server.engine.players) {
		if (pl != null) {
			if (pl.heightLevel == p.heightLevel) {
        			sendCoords(pl, (casterX - ((pl.mapRegionX - 6) * 8)) - 3, (casterY - ((pl.mapRegionY - 6) * 8)) - 2);
        			pl.getByteVector().createFrame(112);
        			pl.getByteVector().writeByte(angle);
        			pl.getByteVector().writeByte(offsetX);
        			pl.getByteVector().writeByte(offsetY); 
        			pl.getByteVector().writeWordBigEndian(lockon); 
        			pl.getByteVector().writeWord(gfxMoving); 
        			pl.getByteVector().writeByte(startHeight); 
        			pl.getByteVector().writeByte(endHeight); 
        			pl.getByteVector().writeWord(33); 
        			pl.getByteVector().writeWord(speed); 
        			pl.getByteVector().writeByte(16);
        			pl.getByteVector().writeByte(64);
			}
		}
	}						
    }

    public void addProjectile4(Player p, int casterY, int casterX, int offsetY, int offsetX, int angle,
            int speed, int gfxMoving, int startHeight, int endHeight, int lockon, boolean npc) {
        if (p == null || p.stream == null) {
            return;
        }
	for (Player pl : Server.engine.players) {
		if (pl != null) {
			if (pl.heightLevel == p.heightLevel) {
        			sendCoords(pl, (casterX - ((pl.mapRegionX - 6) * 8)) - 3, (casterY - ((pl.mapRegionY - 6) * 8)) - 2);
        			pl.getByteVector().createFrame(112);
        			pl.getByteVector().writeByte(angle);
        			pl.getByteVector().writeByte(offsetX);
        			pl.getByteVector().writeByte(offsetY); 
        			pl.getByteVector().writeWordBigEndian(lockon); 
        			pl.getByteVector().writeWord(gfxMoving); 
        			pl.getByteVector().writeByte(startHeight); 
        			pl.getByteVector().writeByte(endHeight); 
        			pl.getByteVector().writeWord(51); 
        			pl.getByteVector().writeWord(speed); 
        			pl.getByteVector().writeByte(0);
        			pl.getByteVector().writeByte(100);
			}
		}
	}						
    }

    public void addProjectile5(Player p, int casterY, int casterX, int offsetY, int offsetX, int angle,
            int speed, int gfxMoving, int startHeight, int endHeight, int lockon, boolean npc) {
        if (p == null || p.stream == null) {
            return;
        }
	for (Player pl : Server.engine.players) {
		if (pl != null) {
			if (pl.heightLevel == p.heightLevel) {
        			sendCoords(pl, (casterX - ((pl.mapRegionX - 6) * 8)) - 3, (casterY - ((pl.mapRegionY - 6) * 8)) - 2);
        			pl.getByteVector().createFrame(112);
        			pl.getByteVector().writeByte(angle);
        			pl.getByteVector().writeByte(offsetX);
        			pl.getByteVector().writeByte(offsetY); 
        			pl.getByteVector().writeWordBigEndian(lockon); 
        			pl.getByteVector().writeWord(gfxMoving); 
        			pl.getByteVector().writeByte(startHeight); 
        			pl.getByteVector().writeByte(endHeight);
			    if (p.fightStyle != 3) {
        			pl.getByteVector().writeWord(21); 
			    } else {
        			pl.getByteVector().writeWord(41); 
			    }
        			pl.getByteVector().writeWord(speed); 
        			pl.getByteVector().writeByte(16);
        			pl.getByteVector().writeByte(64);
			}
		}
	}						
    }

    /**
     * Set the run energy on the client.
     * @param p The Player which the frame should be created for.
     */
    public void setEnergy(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
	p.getByteVector().createFrame(99);
 	p.getByteVector().writeByte(p.runEnergy);
    }

    /**
     * Setting a tab.
     * @param p The Player which the frame should be created for.
     * @param tabId Which tab to display the interface on.
     * @param childId The interface to display on the tab.
     */
    public void setTab(Player p, int tabId, int childId) {
	if (!p.Resizable) {
        	setInterface(p, 1, childId == 137 ? 752 : 548, tabId, childId);
    }
	if (p.Resizable) {
        	setInterface(p, 1, childId == 137 ? 752 : 746, tabId+14, childId);
    }
}

    /**
     * Set the overlay to be displayed.
     * @param p The Player which the frame should be created for.
     * @param childId The interface id to display as an overlay.
     */
    public void setOverlay(Player p, int childId) {
		if (!p.Resizable) {
        setInterface(p, 1, 548, 5, childId);
		    }
		if (p.Resizable) {
	setInterface(p, 1, 746, 5, childId);
    }
}
/**
	* Remove Player Equipment
	*@param p The player which the frame should be created for.
	*@param index the index of the player
	*/
	 public void removeEquipment(Player p, int itemId, int index)
    {
        if(p == null || itemId <= 0 || index < 0 || index > 12 || p.stream == null)
        {
            return;
        }

        PlayerItems pi = new PlayerItems();

        if(!pi.addItem(p, p.equipment[index], p.equipmentN[index]))
        {
            sendMessage(p, "Not enough space in your inventory.");
            return;
        }

        p.equipment[index] = -1;
        p.equipmentN[index] = -1;
        p.stream.createFrameVarSizeWord(135);
        p.stream.writeByte(1);
        p.stream.writeByte(131);
        p.stream.writeByte(0);
        p.stream.writeByte(28);
        p.stream.writeWord(28);
        p.stream.writeByte(index);
        p.stream.writeWord(0);
        p.stream.writeByte(0);
        p.stream.endFrameVarSizeWord();
        p.appearanceUpdateReq = true;
        p.updateReq = true;
        p.calculateEquipmentBonus();
        pi = null;
        PlayerWeapon pweapon = new PlayerWeapon(p);
        pweapon.setWeapon();
        pweapon = null;
    }

    /**
     * Remove any overlays that might be set.
     * @param p The Player which the frame should be created for.
     */
    public void removeOverlay(Player p) {
    setInterface(p, 1, 548, 5, 56);
	setInterface(p, 1, 746, 5, 56);
    }

    /**
     * Display an interface on the main area in the screen.
     * @param p The Player which the frame should be created for.
     * @param childId the interface id to display.
     */
    public void showInterface(Player p, int childId) {
       p.interfaceId = childId;
	           for (int i = 0; i < interfaces.length; i++) {
                if(childId == interfaces[i])
                        p.HDFS = true;
        }
        if(!p.Resizable)
        {
        setInterface(p, 0, 548, 8, childId);
        }
        else if(p.Resizable && p.HDFS == false)
        {
        setInterface(p, 0, 746, 4, childId);
        }
		else if(p.Resizable && p.HDFS == true)
        {
        setInterface(p, 0, 746, 6, childId);
		p.HDFS = false;
        }
    }

    /**
     * Remove an interface on the main screen.
     * @param p The Player which the frame should be created for.
     */
    public void removeShownInterface(Player p) {
        setInterface(p, 1, 548, 8, 56);
        setInterface(p, 1, 746, 4, 56);
        setInterface(p, 1, 746, 6, 56);
        p.interfaceId = -1;
    }

    /**
     * Display an interface on the chatbox.
     * @param p The Player which the frame should be created for.
     * @param childId The interface to display on the chatbox.
     */
    public void showChatboxInterface(Player p, int childId) {
        setInterface(p, 0, 752, 12, childId);
        p.chatboxInterfaceId = childId;
    }

    /**
     * Set the chatbox back removing any interfaces on it.
     * @param p The Player which the frame should be created for.
     */
    public void removeChatboxInterface(Player p) {
        setConfig(p, 334, 1);
        p.stream.createFrame(246);
        p.stream.writeWord(752);
        p.stream.writeWord(12);
        p.chatboxInterfaceId = -1;
    }
    /**
     * Set the inventory.
     * @param p The Player which the frame should be created for.
     * @param childId The interface to display on the inventory.
     */
    /*public void setInventory(Player p, int childId) {
	    setInterface(p, 0, 548, 71, childId);
		setInterface(p, 1, 746, 71, childId);
    }*/
	public void setInventory(Player p, int childId) {
		setInterface(p, 0, 548, 69, childId);
		setInterface(p, 1, 746, 71, childId);
		p.hideTabs(p);
    }
	public void restoreInventory(Player p) {
		setInterface(p, 1, 548, 69, 56);
		setInterface(p, 1, 746, 71, 56);
		p.restoreTabs(p);
    }	
	    public void setInterfaces(Player p) {
        if (p == null || p.disconnected[0]) {
            return;
        }
		if (!p.Resizable) {
        setTab(p, 6, 745);
		setTab(p, 7, 754);
        setTab(p, 11, 751); // Chat options
        setTab(p, 68, 752); // Chatbox
        setTab(p, 64, 748); // HP bar
        setTab(p, 65, 749); // Prayer bar
        setTab(p, 66, 750); // Energy bar
		setTab(p, 67, 747);
		setInterfaceConfig(p, 747, 0, false);
		setInterfaceConfig(p, 747, 1, false);
		setInterfaceConfig(p, 747, 2, false);
		setInterfaceConfig(p, 747, 3, false);
		setInterfaceConfig(p, 747, 4, false);
		setInterfaceConfig(p, 747, 5, false);
        setTab(p, 8, 137); // Playername on chat
        setTab(p, 73,  92); // Attack tab
        setTab(p, 74, 320); // Skill tab
        setTab(p, 75, 274); //  Quest tab	
	p.frames.setString(p, "      <col=0000FF>DangerZone 508</col>", 274, 6);		
 	p.frames.setString(p, "Forums: <col=FFFFFF>www.danger508.com", 274, 7);				
 	if(p.rights == 0) {
		p.frames.setString(p, "Rank:"+" "+" "+"<col=FFFFFF>Player", 274, 8);
	} else if (p.rights == 1) {
		p.frames.setString(p, "Rank:"+" "+" "+"<col=FFFFFF>Moderator", 274, 8);
	} else if (p.rights == 2) {
		p.frames.setString(p, "Rank:"+" "+" "+"<col=FFFFFF>Administrator", 274, 8);
	}		
 	p.frames.setString(p, "Username:"+" "+" "+"<col=FFFFFF>"+p.username.substring(0, 1).toUpperCase() + p.username.substring(1), 274, 9);				
 	p.frames.setString(p, "                 <col=0000ff><u>Teleports</u></col>", 274, 10);
	p.frames.setString(p, "<col=FFFFFF>Home", 274, 11);		
 	p.frames.setString(p, "<col=FFFFFF>Edgeville", 274, 12);
 	p.frames.setString(p, "<col=FFFFFF>Catherby", 274, 13);		
 	p.frames.setString(p, "<col=FFFFFF>Agility Course", 274, 14);
	p.frames.setString(p, "<col=FFFFFF>Warrior's Guild", 274, 15);					
	p.frames.setString(p, "<col=FFFFFF>Fight Pits", 274, 16);	
 	p.frames.setString(p, "<col=FFFFFF>GodWars", 274, 17);		
 	p.frames.setString(p, "<col=FFFFFF>Mage Bank", 274, 18);		
 	p.frames.setString(p, "<col=FFFFFF>Varrock Wildy", 274, 19);		
 	p.frames.setString(p, "<col=FFFFFF>Green Dragons", 274, 20);		
 	p.frames.setString(p, "<col=FFFFFF>Duel Arena", 274, 21);		
 	p.frames.setString(p, "<col=FFFFFF>Bounty Hunter", 274, 22);		
 	p.frames.setString(p, "<col=FFFFFF>Castle Wars", 274, 23);		
 	p.frames.setString(p, "<col=FFFFFF>Grand Exchange", 274, 24);		
 	p.frames.setString(p, "           <col=0000ff>Quests</col>", 274, 25);
	p.frames.setString(p, "", 274, 26);
if (p.q1 == 0) {
		setString(p, "The Beginning", 274, 26);
			} else if (p.q1 == 1) {
		setString(p, "<col=FFFF00>The Beginning", 274, 26);
			} else if (p.q1 == 15) {
		setString(p, "<col=00FF00>The Beginning", 274, 26);
			}
if (p.StraightChamp == 0) {
			setString(p, "Straight Champion", 274, 131);
		} else if (p.StraightChamp > 0 && p.StraightChamp < 1000) {
			setString(p, "<col=FFFF00>Straight Champion", 274, 131);
		} else if (p.StraightChamp >= 999) {
			setString(p, "<col=00FF00>Straight Champion", 274, 131);
		}
	p.frames.setString(p, "", 274, 140);	
	p.frames.setString(p, "Turmoil", 274, 136);
	p.frames.setString(p, "Soul Split", 274, 27);
	p.frames.setString(p, "Coming Soon!", 274, 28);	
	p.frames.setString(p, "Coming Soon!", 274, 29);
	p.frames.setString(p, "Coming Soon!", 274, 30);

        setTab(p, 76, 149); // Inventory tab
        setTab(p, 77, 387); // Equipment tab
        setTab(p, 78, 271); // Prayer tab
        setTab(p, 79, 192); // Magic tab
        setTab(p, 81, 550); // Friend tab
        setTab(p, 82, 551); // Ignore tab
        setTab(p, 83, 589); // Clan tab
        setTab(p, 84, 261); // Setting tab
        setTab(p, 85, 464); // Emote tab
        setTab(p, 86, 187); // Music tab
        setTab(p, 87, 182); // Logout tab
    }
	if (p.Resizable) {
	    RemovesetInterfaces(p);
    setInterface(p, 1, 549, 0, 746); 
    setInterface(p, 1, 746, 13, 748); //energy orb
    setInterface(p, 1, 746, 14, 749); //energy orb
    setInterface(p, 1, 746, 15, 750); //energy orb
	setInterface(p, 1, 746, 16, 747); //summing orb
    setInterface(p, 1, 746, 18, 751); //things below chatbox 
    setInterface(p, 1, 752, 8, 137); //chatbox
    setInterface(p, 1, 746, 65, 752); //chatbox 752
    setInterface(p, 1, 549, 0, 746); // Main interface
    setInterface(p, 1, 746, 87, 92); // Attack tab
	p.playerWeapon.setWeapon();
    setInterface(p, 1, 746, 88, 320); // Skill tab
    setInterface(p, 1, 746, 89, 274); // Quest tab //13
	if (p.StraightChamp == 0) {
		setString(p, "<col=FF0000>Straight Champion", 274, 5);
	} else if (p.StraightChamp > 0 && p.StraightChamp < 1000) {
		setString(p, "<col=FFFF00>Straight Champion", 274, 5);
	} else if (p.StraightChamp >= 999) {
		setString(p, "<col=00FF00>Straight Champion", 274, 5);
	}
    setInterface(p, 1, 746, 90, 149); // Inventory tab
    setInterface(p, 1, 746, 91, 387); // Equipment tab
    setInterface(p, 1, 746, 92, 271); // Prayer tab
    setInterface(p, 1, 746, 93, 192); // Magic tab
    setInterface(p, 1, 746, 94, 662); // Summoning tab
    setInterface(p, 1, 746, 95, 550); // Friend tab
    setInterface(p, 1, 746, 96, 551); // Ignore tab
    setInterface(p, 1, 746, 97, 589); // Clan tab
    setInterface(p, 1, 746, 98, 261); // Setting tab
    setInterface(p, 1, 746, 99, 464); // Emote tab
    setInterface(p, 1, 746, 100, 187); // Music tab
    setInterface(p, 1, 746, 101, 182); // Logout tab
    setInterface(p, 1, 752, 8, 137); // Chatbox 
    setInterface(p, 1, 746, 65, 752); // Chatbox 752
    setInterface(p, 1, 746, 18, 751); // Settings below chatbox
    setInterface(p, 1, 746, 13, 748); // HP orb
    setInterface(p, 1, 746, 14, 749); // Prayer orb
    setInterface(p, 1, 746, 15, 750); // Energy orb
        for (int i = 0; i < 6; i++)
        	p.frames.setInterfaceConfig(p, 747, i, false);
        p.frames.setConfig(p, 1160, -1);
    }
	    }

    /*public void setFullScreenInterfaces(Player p) {
        if (p == null || p.disconnected[0]) {
            return;
        }
    RemovesetInterfaces(p);
    setInterface(p, 1, 549, 0, 746); 
	setInterface(p, 1, 746, 12, 754);//split pm
    setInterface(p, 1, 746, 13, 748); //energy orb
    setInterface(p, 1, 746, 14, 749); //energy orb
    setInterface(p, 1, 746, 15, 750); //energy orb
	    setInterface(p, 1, 746, 16, 747); //summing orb
    setInterface(p, 1, 746, 18, 751); //things below chatbox 
    setInterface(p, 1, 752, 8, 137); //chatbox
    setInterface(p, 1, 746, 65, 752); //chatbox 752
    setInterface(p, 1, 549, 0, 746); // Main interface
    setInterface(p, 1, 746, 87, 92); // Attack tab
	p.playerWeapon.setWeapon();
    setInterface(p, 1, 746, 88, 320); // Skill tab
    setInterface(p, 1, 746, 89, 274); // Quest tab
    setInterface(p, 1, 746, 90, 149); // Inventory tab
    setInterface(p, 1, 746, 91, 387); // Equipment tab
    setInterface(p, 1, 746, 92, 271); // Prayer tab
    setInterface(p, 1, 746, 93, 192); // Magic tab
    setInterface(p, 1, 746, 94, 662); // Summoning tab
    setInterface(p, 1, 746, 95, 550); // Friend tab
    setInterface(p, 1, 746, 96, 551); // Ignore tab
    setInterface(p, 1, 746, 97, 589); // Clan tab
    setInterface(p, 1, 746, 98, 261); // Setting tab
    setInterface(p, 1, 746, 99, 464); // Emote tab
    setInterface(p, 1, 746, 100, 187); // Music tab
    setInterface(p, 1, 746, 101, 182); // Logout tab
    setInterface(p, 1, 752, 8, 137); // Chatbox 
    setInterface(p, 1, 746, 65, 752); // Chatbox 752
    setInterface(p, 1, 746, 18, 751); // Settings below chatbox
    setInterface(p, 1, 746, 13, 748); // HP orb
    setInterface(p, 1, 746, 14, 749); // Prayer orb
    setInterface(p, 1, 746, 15, 750); // Energy orb
        for (int i = 0; i < 6; i++)
        	p.frames.setInterfaceConfig(p, 747, i, false);
        p.frames.setConfig(p, 1160, -1);
    }*/

    /**
     * Send coordinates, used with other frames.
     * @param p The Player which the frame should be created for.
     * @param x The x coordinate.
     * @param y The y coordinate.
     */
    public void sendCoords(Player p, int x, int y) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(177);
        p.getByteVector().writeByte(y);
        p.getByteVector().writeByteS(x);
    }

    /**
     * Creates an item on the ground at itemX and itemY.
     * @param p The Player which the frame should be created for.
     * @param itemId The item id to be displayed.
     * @param itemAmt The amount the item stack size is.
     * @param itemX The absolute x coordinate to display the item.
     * @param itemY The absolute y coordinate to display the item.
     * @param itemHeight The height level to set the item.
     */
    public void createGroundItem(Player p, int itemId, int itemAmt, int itemX, int itemY, int itemHeight) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        if (Misc.getDistance(itemX, itemY, p.absX, p.absY) <= 60 && p.heightLevel == itemHeight) {
            sendCoords(p, (itemX - ((p.mapRegionX - 6) * 8)), (itemY - ((p.mapRegionY - 6) * 8)));
            p.getByteVector().createFrame(25);
            p.getByteVector().writeWordBigEndianA(itemAmt);
            p.getByteVector().writeByte(0);
            p.getByteVector().writeWordBigEndianA(itemId);
        }
    }

    /**
     * Removes an item on the ground at itemX and itemY.
     * @param p The Player which the frame should be created for.
     * @param itemId The item id to remove.
     * @param itemX The absolute x coordinate to remove the item.
     * @param itemY The absolute y coordinate to remove the item.
     * @param itemHeight The height level toe remove the item at.
     */
    public void removeGroundItem(Player p, int itemId, int itemX, int itemY, int itemHeight) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        if(Misc.getDistance(itemX, itemY, p.absX, p.absY) <= 60 && p.heightLevel == itemHeight) {
            sendCoords(p, (itemX - ((p.mapRegionX - 6) * 8)), (itemY - ((p.mapRegionY - 6) * 8)));
            p.getByteVector().createFrame(201);
            p.getByteVector().writeByte(0);
            p.getByteVector().writeWord(itemId);
        }
    }
    /**
     * Remove Player Equipment
     * @param p The player which the frame should be created for.
     * @param index the index of the player
     */
     /* public void removeEquipment(Player p, int itemId, int index) {
        if(p == null || itemId <= 0 || index < 0 || index > 13 || p.stream == null)
        {
            return;
        }
        PlayerItems pi = new PlayerItems();
        if(!pi.addItem(p, p.equipment[index], p.equipmentN[index]))
        {
            sendMessage(p, "Not enough space in your inventory.");
            return;
        }
        p.equipment[index] = -1;
        p.equipmentN[index] = 0;
        p.getByteVector().createFrameVarSizeWord(135);
        p.getByteVector().writeByte(1);
        p.getByteVector().writeByte(131);
        p.getByteVector().writeByte(0);
        p.getByteVector().writeByte(28);
        p.getByteVector().writeWord(28);
        p.getByteVector().writeByte(index);
        p.getByteVector().writeWord(0);
        p.getByteVector().writeByte(0);
        p.getByteVector().endFrameVarSizeWord();
        p.appearanceUpdateReq = true;
        p.updateReq = true;
        p.calculateEquipmentBonus();
        PlayerWeapon pweapon = new PlayerWeapon(p);
        pweapon.setWeapon();
    } */

    /**
     * Send players stat.
     * @param p The Player which the frame should be created for.
     * @param lvlId The stat id to send.
     */
    public void setSkillLvl(Player p, int lvlId) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(217);
        p.getByteVector().writeByteC(p.skillLvl[lvlId]);
        p.getByteVector().writeDWord_v2(p.skillXP[lvlId]);
        p.getByteVector().writeByteC(lvlId);
    }

    /**
     * Set item display on an interface.
     * @param p The Player which the frame should be created for.
     * @param interfaceId The interface to display the items on.
     * @param childId The child interface on the main interface.
     * @param itemArray The item id array to set on the interface.
     * @param itemAmt The item array to go with the itemArray.
     */
    public void setItems(Player p, int interfaceId, int childId, int type, int[] itemArray, int[] itemAmt) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSizeWord(255);
        p.getByteVector().writeWord(interfaceId);
        p.getByteVector().writeWord(childId);
        p.getByteVector().writeWord(type);
        p.getByteVector().writeWord(itemArray.length);
        for (int i = 0; i < itemArray.length; i++) {
            if (itemAmt[i] > 254) {
                p.getByteVector().writeByteS(255);
                p.getByteVector().writeDWord_v2(itemAmt[i]);
            } else {
                p.getByteVector().writeByteS(itemAmt[i]);
            }
            p.getByteVector().writeWordBigEndian(itemArray[i] + 1);
        }
        p.getByteVector().endFrameVarSizeWord();
    }

    /**
     * Set interface configs.
     * <p>This is used to do things such as hiding and displaying the special attack bar.
     * @param p The Player which the frame should be created for.
     * @param interfaceId The interface to the set the config with.
     * @param childId The child that belongs to the interface to change.
     * @param 1 for true, 0 for false.
     */
    public void setInterfaceConfig(Player p, int interfaceId, int childId, boolean set) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrame(59);
        p.getByteVector().writeByteC(set ? 1 : 0);
        p.getByteVector().writeWord(childId);
        p.getByteVector().writeWord(interfaceId);
    }

    /**
     * Display a message in the chatbox.
     * @param p The Player which the frame should be created for.
     * @param s The message to display in the chatbox.
     */
    public void sendMessage(Player p, String s) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSize(218);
        p.getByteVector().writeString(s);
        p.getByteVector().endFrameVarSize();
    }

    /**
     * Set a string on an interface.
     * @param p The Player which the frame should be created for.
     * @param str The string to set on the interface.
     * @param interfaceId The interface to set the text on.
     * @param childId The interface's child to set the text on.
     */
    public void setString(Player p, String str, int interfaceId, int childId) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        int sSize = str.length() + 5;
        p.getByteVector().createFrame(179);
        p.getByteVector().writeByte(sSize / 256);
        p.getByteVector().writeByte(sSize % 256);
        p.getByteVector().writeString(str);
        p.getByteVector().writeWord(childId);
        p.getByteVector().writeWord(interfaceId);
    }

    /**
     * Send this player's updated coordinates.
     * @param p The Player which the frame should be created for.
     */
    public void updateMovement(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSizeWord(216);
        p.getByteVector().initBitAccess();
        p.getByteVector().writeBits(1, 1);
        if (p.runDir == -1) {
            p.getByteVector().writeBits(2, 1);
            p.getByteVector().writeBits(3, p.walkDir);
            p.getByteVector().writeBits(1, p.updateReq ? 1 : 0);
        } else {
            p.getByteVector().writeBits(2, 2);
            p.getByteVector().writeBits(3, p.runDir);
            p.getByteVector().writeBits(3, p.walkDir);
            p.getByteVector().writeBits(1, p.updateReq ? 1 : 0);
            if (p.runEnergy > 0) {
                p.runEnergyUpdateReq = true;
                p.runEnergy--;
            } else {
                p.isRunning = false;
            }
        }
    }

    /**
     * Tell the client this player isn't moving.
     * @param p The Player which the frame should be created for.
     */
    public void noMovement(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSizeWord(216);
        p.getByteVector().initBitAccess();
        p.getByteVector().writeBits(1, p.updateReq ? 1 : 0);
        if (p.updateReq) {
            p.getByteVector().writeBits(2, 0);
        }
    }

    /**
     * Changes the coordinates this player is standing at.
     * @param p The Player which the frame should be created for.
     */
    public void teleport(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSizeWord(216);
        p.getByteVector().initBitAccess();
        p.getByteVector().writeBits(1, 1);
        p.getByteVector().writeBits(2, 3);
        p.getByteVector().writeBits(7, p.currentX);
        p.getByteVector().writeBits(1, 1);
        p.getByteVector().writeBits(2, p.heightLevel);
        p.getByteVector().writeBits(1, p.updateReq ? 1 : 0);
        p.getByteVector().writeBits(7, p.currentY);
    }

    /**
     * Send the map region and other positioning info to the client.
     * @param p The Player which the frame should be created for.
     */
    public void setMapRegion(Player p) {
        if (p == null || p.stream == null || p.disconnected[0]) {
            return;
        }
        p.getByteVector().createFrameVarSizeWord(142);
        p.getByteVector().writeWordA(p.mapRegionX);
        p.getByteVector().writeWordBigEndianA(p.currentY);
        p.getByteVector().writeWordA(p.currentX);
        boolean forceSend = true;
        if ((((p.mapRegionX / 8) == 48) || ((p.mapRegionX / 8) == 49)) && ((p.mapRegionY / 8) == 48)) {
            forceSend = false;
        }
        if (((p.mapRegionX / 8) == 48) && ((p.mapRegionY / 8) == 148)) {
            forceSend = false;
        }
        for (int xCalc = (p.mapRegionX - 6) / 8; xCalc <= ((p.mapRegionX + 6) / 8); xCalc++) {
            for (int yCalc = (p.mapRegionY - 6) / 8; yCalc <= ((p.mapRegionY + 6) / 8); yCalc++) {
       	int region = yCalc + (xCalc << 1786653352);
                	if (forceSend || ((yCalc != 49) && (yCalc != 149) && (yCalc != 147) && (xCalc != 50) && ((xCalc != 49) || (yCalc != 47)))) {
                    	int[] mapData = Engine.mapData.getMapData(region);
		if (mapData == null) {
			p.setCoords(3254, 3420, 0);
			sendMessage(p, "You got teleported to varrock due to Mapdata missing.");
			return;
		}
                    p.getByteVector().writeDWord(mapData[0]);
                    p.getByteVector().writeDWord(mapData[1]);
                    p.getByteVector().writeDWord(mapData[2]);
                    p.getByteVector().writeDWord(mapData[3]);
                }
            }
        }
        p.getByteVector().writeByteC(p.heightLevel);
        p.getByteVector().writeWord(p.mapRegionY);
        p.getByteVector().endFrameVarSizeWord();
    }
}